public class SingleLinkedList implements IList {

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        ListNode cur;
        cur = head;
        int count = 0;
        if(head == null) {
            head = node;
            last = node;
        }else {
            while (cur.val != index) {
                cur = cur.next;
                count++;
            }
            node.next = cur.next;
            cur.next = node;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode node = new ListNode(key);
        ListNode cur;
        cur = head;
        while(cur != null) {
            if(cur == node) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur.next.val != key) {
            cur = cur.next;
        }
        ListNode del = cur.next;
        if(cur.next == null) {
          System.out.println("不包含删除元素!");
        }else {
            cur.next = del.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }

    public static void main(String[] args) {
        SingleLinkedList link = new SingleLinkedList();
        link.addFirst(1);
        link.addLast(3);
        link.addLast(4);
        link.display();
        link.addIndex(1,6);
        link.addLast(3);
        link.addLast(3);
        link.display();
        link.contains(3);
        link.remove(4);
        link.display();
        link.removeAllKey(3);
        link.display();
        link.clear();
        link.display();
    }
}